package br.com.nooclix.ptt.ws.application;

import java.util.Date;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import br.com.nooclix.ptt.core.application.AdminFinancialApplication;
import br.com.nooclix.ptt.core.application.AffiliateApplication;
import br.com.nooclix.ptt.core.domain.admin.financial.Transfer;
import br.com.nooclix.ptt.core.domain.afiliate.Affiliate;
import br.com.nooclix.ptt.core.infrastructure.util.ApplicationContextRegisterCore;
import br.com.nooclix.ptt.ws.adapter.Adapter;
import br.com.nooclix.ptt.ws.adapter.impl.TransferAdapter;
import br.com.nooclix.ptt.ws.utilities.Utilities;

@Path("/transfer")
public class AdminFinancialRestApplication {

	private AdminFinancialApplication adminFinancialApplication;
	private AffiliateApplication affiliateApplication;
	private Adapter<Transfer> adapter;

	public AdminFinancialRestApplication() {
		super();
		adminFinancialApplication = ApplicationContextRegisterCore.getBean(
				"adminFinancialApplication", AdminFinancialApplication.class);
		adapter = new TransferAdapter();
	}

	@GET
	@Path("/list/date/{beginStringDate}/{endStringDate}/{offset}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response listTransferByDate(
			@PathParam("beginStringDate") String beginStringDate,
			@PathParam("endStringDate") String endStringDate,
			@PathParam("offset") int offset) {
		Response response = null;
		Date beginDate = Utilities.getDateFromString(beginStringDate);
		Date endDate = Utilities.getDateFromString(endStringDate);
		List<Transfer> transfers = adminFinancialApplication.listByDate(
				beginDate, endDate, offset);
		String json = adapter.convert(transfers);
		response = Response.ok(json).build();
		return response;
	}
	
	@GET
	@Path("/find/nid/{nid}/{beginStringDate}/{endStringDate}/{offset}")
	public Response findTransferByAffiliateNID(@PathParam("nid") String nid,@PathParam("beginStringDate") String beginStringDate,
			@PathParam("endStringDate") String endStringDate,
			@PathParam("offset") int offset){
		Date beginDate = Utilities.getDateFromString(beginStringDate);
		Date endDate = Utilities.getDateFromString(endStringDate);
		List<Transfer> transfers = adminFinancialApplication.findTransferByAffiliateNID(nid, beginDate, endDate, offset);
		String json = adapter.convert(transfers);
		Response response = Response.ok(json).build();
		return response;
	}
	
	@GET
	@Path("/find/name/{name}/{beginStringDate}/{endStringDate}/{offset}")
	public Response findTransferByAffiliateName(@PathParam("name") String name,@PathParam("beginStringDate") String beginStringDate,
			@PathParam("endStringDate") String endStringDate,
			@PathParam("offset") int offset ){
		Response response = null;
		Date beginDate = Utilities.getDateFromString(beginStringDate);
		Date endDate = Utilities.getDateFromString(endStringDate);
		List<Transfer> transfer = adminFinancialApplication.findTransferByAffiliateName(name, beginDate, endDate, offset);
		String json = adapter.convert(transfer);
		response = Response.ok(json).build();
		return response;
	}

}
